﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;
using System.IO;

//using ColourAdvisorPhoneApp.ColourAdvisorServiceReference; // local host service reference
using ColourAdvisorPhoneApp.FashionColourAdvisorCloudServiceReference; // Azure cloud service reference

namespace ColourAdvisorPhoneApp
{
    public partial class ColourTypeImagesPage : PhoneApplicationPage
    {
        // method to get description attribute of ColourTypeName enums so displayed with spacing e.g. Deep Winter rather than DeepWinter
        // this method is also on colourresult page so how to not repeat it?
        public static string GetEnumDescription(Enum value)
        {
            System.Reflection.FieldInfo field = value.GetType().GetField(value.ToString());

            System.ComponentModel.DescriptionAttribute[] attributes =
                (System.ComponentModel.DescriptionAttribute[])field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }
        
        public ColourTypeImagesPage()
        {
            InitializeComponent();

            // Access App class for user colour type in isolated storage
            ColourTypeName colourTypeResult = (Application.Current as App).ColourType;

            // use Enum.GetName to get string value of the enum colourTypeResult  
            // string name = Enum.GetName(typeof(ColourTypeName), colourTypeResult);

            // method to get description attribute of ColourTypeResult
            String name = GetEnumDescription(colourTypeResult);

            string colourTypeText = name;
            // display colour type to text block on phone screen
            colourTypeTextBlock.Text = colourTypeText;


            // construct absolute Url to map to celebrity image in blob storage
            String imageUrlCelebrity = colourTypeResult.ToString() + "_Celebrity.jpg";
            // assign this Url to source property of image
            celebrityImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://fashioncolouradvisor.blob.core.windows.net/images/" + imageUrlCelebrity, UriKind.Absolute));

            // construct absolute Url to map to colour images in blob storage
            String imageUrlColours = colourTypeResult.ToString() + "_Colours.jpg";
            // assign this Url to source property of image
            coloursImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://fashioncolouradvisor.blob.core.windows.net/images/" + imageUrlColours, UriKind.Absolute));
        }

        # region Application Bar Events

        private void BackButton_Click(object sender, EventArgs e)
        {
            // Navigate to previous page for description of Colour Type
            NavigationService.Navigate(new Uri("/ColourResultPage.xaml", UriKind.Relative));
        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            /// Navigate to Fashion Trends page
            NavigationService.Navigate(new Uri("/FashionTrendsPage.xaml", UriKind.Relative));
        }

        private void IntroItem_Click(object sender, EventArgs e)
        {
            // Navigate to Intro page i.e. MainPage
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void DiscoverItem_Click(object sender, EventArgs e)
        {
            //Navigate to Colouring Details Page to Start "Discover My Colour Type"
            NavigationService.Navigate(new Uri("/ColouringDetailsPage.xaml", UriKind.Relative));
        }

        private void ColourTypeDescriptionItem_Click(object sender, EventArgs e)
        {
            //Navigate to My Colour Type Description page
            NavigationService.Navigate(new Uri("/ColourResultPage.xaml", UriKind.Relative));
        }

        private void FashionTrendsItem_Click(object sender, EventArgs e)
        {
            // Navigate to Fashion Trends page
            NavigationService.Navigate(new Uri("/FashionTrendsPage.xaml", UriKind.Relative));
        }

        # endregion 
    }
}